/**
 * 重庆市政府采购网爬虫
 *  
 */
import {Base,Item} from '../base';
class cqzfcg extends Base{
    constructor(){
        super();
        this.name = "重庆市政府采购网";
        this.enabled = true;
        this.encode = "utf-8";
        this.debug = false;
        this.timeout = 1000000;
        this.collection = "ztb";
        this.collectionShowName = "招投标信息";
        this.keyField = "title";
    }
    getItems(pageIndex:number):Item[]{
        let result :Item[]= [];
        let num=pageIndex+1;
        let url = `https://www.cqgp.gov.cn/gwebsite/api/v1/notices/stable?pi=${num}&ps=20&timestamp=1515479191595&type=100,200,201,202,203,204,205,206,400,401,4001&userType=41`;
        let content=this.spider.getSync(url);
       /*  let $=this.cheerio.load(content);
        let that =this;
        $('.vF_detail_relcontent_lst ul li').each(function(index,ele){
            let item =that.buildItem();
            item.title=$(ele).find('a').text().trim();
            (<any>item)['采购形式']=$(ele).find('em').eq(0).text().trim();
            (<any>item)['采购日期']=$(ele).find('em').eq(1).text().trim().substring(1,11);
            item.source=that.name;
            let uri='http://www.ccgp.gov.cn/cggg/dfgg'+$(ele).find('a').attr('href').substring(1);
            (<any>item).url='http://www.ccgp.gov.cn/cggg/dfgg'+$(ele).find('a').attr('href').substring(1);
            result=that.getExtraItem(uri,item,result);
        }); */
        let that =this;
        let jsonData=(<any>content).notices;
        for(let k in jsonData){
            let item=this.buildItem();
            item.title=jsonData[k].title;
            item.source=that.name;


            
        }


        return result;
    }
    getExtraItem(url:string, item:Item, result:Item[]) {
        let content = this.spider.getSync(url);
        let $ = this.cheerio.load(content);
        (<any>item).content = $('table').html();
        result.push(item);
        return result;
    }
    getPageCount() {
        return 20;
    }
}
module.exports=new cqzfcg();